const {
    parseStudentId
} = require('../../utils/helpers.js');

module.exports = async (ctx, next) => {
    const { query,params} = ctx;
    if(params.model==='student'){
        const toRemove = Object.keys(query).reduce((acc, curr) => {
            if (curr !== 'source') {
                return acc.concat([query[curr]]);
            }
            return acc;
        }, []);
        for(const id of toRemove){
            const student = await strapi.services.student.fetch({id});
            const data = {
                device:student.device.id,
                action:'student-del',
                data:{
                    user_id:parseStudentId(id)
                }
            };
            await strapi.plugins['content-manager'].services['contentmanager'].add({ model: 'task' }, data, 'task');
        }
    }
    await next();

};
